package com.dp.appi.dao.impl;

import com.dp.appi.dao.ChannelDao;
import com.dp.appi.dao.StageDao;
import com.dp.appi.util.RegularUtil;
import com.dp.appi.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by bchen on 2016/5/17.
 */
@Component
public class StageDaoImpl extends BaseDaoImpl implements StageDao {
//    /**
//     * 获取最大编号值
//     *
//     * @param stage_id
//     * @return
//     */
//    @Override
//    public int getMaxNum(Long stage_id) {
//        String sql="select max_num from p_stage " +
//                " where stage_id=?";
//        return getJdbcTemplate().queryForObject(sql,new Object[]{stage_id},Integer.class);
//    }
//
//    /**
//     * 设置最大阶段
//     *
//     * @param stage_id
//     */
//    @Override
//    public void setMaxNum(int num,Long stage_id) {
//        String sql="UPDATE p_stage SET max_num = ? where stage_id = ?";
//        getJdbcTemplate().update(sql,num,stage_id);
//    }

    /**
     * 创建新的踏勘点编号
     *
     * @param stage_id
     * @param pt_id
     * @return
     */
    @Override
    public String newExpNum(final Long stage_id, final Integer pt_id) {

        StringBuffer pSql = new StringBuffer();
        pSql.append("select project_id from p_stage where stage_id = ?");
        final Long project_id = queryForObject(pSql.toString(), Long.class, stage_id);

        StringBuffer iSql = new StringBuffer();
        iSql.append(" INSERT INTO p_exploration_num (project_id, pt_id)");
        iSql.append(" VALUES (:project_id,:pt_id) ");

        StringBuffer qSql = new StringBuffer();
        qSql.append("select CONCAT(b.prefix,'-',a.current_num) num ");
        qSql.append("from p_exploration_num a    ");
        qSql.append("left join p_pt b  ");
        qSql.append("on a.pt_id = b.pt_id  ");
        qSql.append("where project_id = ? and a.pt_id = ?  ");

        String sql = "select pen.current_num from p_exploration_num pen where project_id = ? and pt_id = ?";



        String current_num = queryForObject(sql, String.class, project_id, pt_id);//当前项目的对应点位类型编号的横杠后部分
        if (StringUtil.isEmpty(current_num)) {
            Map map = new HashMap() {
                {
                    put("project_id", project_id);
                    put("pt_id", pt_id);
                }

            };
            executeSqlReturnID(iSql.toString(),map);
            current_num = queryForObject(qSql.toString(), String.class, project_id, pt_id);
        }else{
            //正则表达式验证并截取
            String zz = "^(.*)(\\d+)$";
            Matcher matcher = RegularUtil.getMatcher(zz, current_num);
            if (matcher.matches()) {
                String body = matcher.group(1);//num中前部分字符串
                int i = Integer.parseInt(matcher.group(2));//num中后部分数字
                current_num = body + (i + 1);

                //更新数据库记录
                StringBuffer uSql = new StringBuffer();
                uSql.append("update p_exploration_num set current_num = ?");
                uSql.append("where project_id = ? and pt_id = ? ");
                getJdbcTemplate().update(uSql.toString(), new Object[]{current_num, project_id, pt_id});

                //返回需要的数据
                current_num = queryForObject(qSql.toString(), String.class, project_id, pt_id);

            } else {
                current_num = "p_exploration_num表中数据不符合规则";
            }
        }

//

//        StringBuffer uSql = new StringBuffer();
//        uSql.append("update p_exploration_num set max_num = max_num+1  ");
//        uSql.append("where project_id = ? and pt_id = ? ");
//
//        StringBuffer qSql = new StringBuffer();
//        qSql.append("select CONCAT(b.prefix,'-',a.max_num) num ");
//        qSql.append("from p_exploration_num a    ");
//        qSql.append("left join p_pt b  ");
//        qSql.append("on a.pt_id = b.pt_id  ");
//        qSql.append("where project_id = ? and a.pt_id = ?  ");
//
//
//        String num = queryForObject(qSql.toString(),String.class,project_id,pt_id);
//        if(StringUtil.isEmpty(num)){
//            Map map = new HashMap() {
//                {
//                    put("project_id",project_id);
//                    put("pt_id",pt_id);
//                }
//            };
//            executeSqlReturnID(iSql.toString(),map);
//            num = queryForObject(qSql.toString(),String.class,project_id,pt_id);
//        }
//        getJdbcTemplate().update(uSql.toString(),project_id,pt_id);
//        return num;
        return current_num;
    }


}
